Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 625)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.02793 13.02452 13.02118 13.01792 13.01472 13.01158 13.00849 13.00546
## [9] 13.00246 12.99951 12.99659 12.99370 12.99082 12.98797 12.98512 12.98228
## [17] 12.97944 12.97660 12.97374 12.97087 12.96797 12.96505 12.96209 12.95910
## [25] 12.95606 12.95297 12.94982 12.94662 12.94335 12.94003 12.93668 12.93331
## [33] 12.92992 12.92651 12.92308 12.91965 12.91622 12.91279 12.90936 12.90594
## [41] 12.90253 12.89914 12.89578 12.89243 12.88912 12.88584 12.88260 12.87940
## [49] 12.87624 12.87314 12.87009 12.86710 12.86417 12.86131 12.85852 12.85580
## [57] 12.85316 12.85061 12.84814 12.84577 12.84349 12.84131 12.83924 12.83728
## [65] 12.83532 12.83328 12.83116 12.82898 12.82674 12.82445 12.82212 12.81975
## [73] 12.81736 12.81496 12.81255 12.81015 12.80775 12.80538 12.80303 12.80073
## [81] 12.79846 12.79626 12.79412 12.79205 12.79006 12.78817 12.78637 12.78468
## [89] 12.78311 12.78166 12.78035 12.77918 12.77816 12.77730 12.77661 12.77610
## [97] 12.77577 12.77564 12.77572 12.77579 12.77565 12.77533 12.77483 12.77418
## [105] 12.77341 12.77251 12.77152 12.77046 12.76934 12.76818 12.76700 12.76582
## [113] 12.76465 12.76352 12.76245 12.76145 12.76054 12.75975 12.75908 12.75856
## [121] 12.75821 12.75804 12.75808 12.75834 12.75885 12.75961 12.76066 12.76200
## [129] 12.76366 12.76566 12.76801 12.77073 12.77385 12.77840 12.78521 12.79398
## [137] 12.80441 12.81621 12.82906 12.84268 12.85676 12.87101 12.88511 12.89877
## [145] 12.91170 12.92358 12.93413 12.94304 12.95001 12.95728 12.96708 12.97913
## [153] 12.99311 13.00873 13.02568 13.04367 13.06239 13.08154 13.10083 13.11995
## [161] 13.13859 13.15647 13.17328 13.18871 13.20247 13.21426 13.22378 13.23072
## [169] 13.23639 13.24230 13.24840 13.25465 13.26104 13.26751 13.27405 13.28061
## [177] 13.28715 13.29366 13.30009 13.30640 13.31257 13.31856 13.32434 13.32987
## [185] 13.33512 13.34005 13.34464 13.34884 13.35263 13.35596 13.35881 13.36114
## [193] 13.36293 13.36412 13.36469 13.36461 13.36385 13.36236 13.36011 13.35707
## [201] 13.35322 13.34850 13.34289 13.33636 13.32887 13.32038 13.31087 13.30030
## [209] 13.28863 13.27583 13.26044 13.24132 13.21896 13.19382 13.16638 13.13711
## [217] 13.10648 13.07498 13.04307 13.01123 12.97993 12.94965 12.92086 12.89404
## [225] 12.86965 12.84817 12.82632 12.80076 12.77193 12.74028 12.70624 12.67025
## [233] 12.63276 12.59419 12.55500 12.51562 12.47648 12.43803 12.40071 12.36496
## [241] 12.33121 12.29990 12.27148 12.24638 12.22504 12.20634 12.18882 12.17243
## [249] 12.15710 12.14278 12.12940 12.11692 12.10526 12.09438 12.08422 12.07470
## [257] 12.06579 12.05741 12.04951 12.04203 12.03492 12.02811 12.02154 12.01517
## [265] 12.00892 12.00273 11.99657 11.99035 11.98403 11.97754 11.97083 11.96384
## [273] 11.95650 11.94877 11.94058 11.93292 11.92669 11.92173 11.91787 11.91495
## [281] 11.91280 11.91125 11.91015 11.90931 11.90858 11.90778 11.90676 11.90535
## [289] 11.90338 11.90068 11.89710 11.89245 11.88658 11.87932 11.87140 11.86365
## [297] 11.85604 11.84853 11.84111 11.83374 11.82640 11.81906 11.81169 11.80427
## [305] 11.79676 11.78913 11.78137 11.77345 11.76533 11.75698 11.74767 11.73674
## [313] 11.72433 11.71054 11.69551 11.67936 11.66221 11.64418 11.62541 11.60600
## [321] 11.58609 11.56579 11.54523 11.52454 11.50383 11.48323 11.46287 11.44286
## [329] 11.42332 11.40439 11.38618 11.36882 11.35244 11.33714 11.32306 11.31032
## [337] 11.29905 11.28936 11.28138 11.27523 11.27103 11.26892 11.26901 11.27043
## [345] 11.27228 11.27460 11.27740 11.28075 11.28466 11.28918 11.29435 11.30019
## [353] 11.30675 11.31407 11.32218 11.33111 11.34091 11.35162 11.36325 11.37723
## [361] 11.39468 11.41531 11.43879 11.46479 11.49301 11.52312 11.55481 11.58775
## [369] 11.62162 11.65611 11.69090 11.72567 11.76009 11.79386 11.82665 11.85814
## [377] 11.88801 11.91595 11.94164 11.96475 11.98855 12.01623 12.04730 12.08128
## [385] 12.11770 12.15608 12.19595 12.23682 12.27821 12.31966 12.36069 12.40081
## [393] 12.43956 12.47644 12.51100 12.54274 12.57120 12.59589 12.61635 12.63549
## [401] 12.65635 12.67861 12.70193 12.72598 12.75044 12.77498 12.79926 12.82297
## [409] 12.84576 12.86732 12.88731 12.90541 12.92128 12.93459 12.94503 12.95278
## [417] 12.95839 12.96201 12.96382 12.96399 12.96268 12.96006 12.95631 12.95157
## [425] 12.94604 12.93986 12.93322 12.92627 12.91919 12.91215 12.90530 12.89883
## [433] 12.89289 12.88766 12.88330 12.87738 12.86769 12.85478 12.83916 12.82137
## [441] 12.80194 12.78140 12.76029 12.73913 12.71845 12.69878 12.68066 12.66462
## [449] 12.65118 12.64088 12.63168 12.62123 12.60962 12.59696 12.58331 12.56878
## [457] 12.55345 12.53741 12.52074 12.50354 12.48590 12.46790 12.44963 12.43118
## [465] 12.41265 12.39411 12.37565 12.35737 12.33936 12.32170 12.30448 12.28778
## [473] 12.27171 12.25634 12.24177 12.22809 12.21537 12.20372 12.19321 12.18395
## [481] 12.17601 12.16949 12.16447 12.16038 12.15662 12.15320 12.15013 12.14745
## [489] 12.14517 12.14332 12.14191 12.14096 12.14050 12.14054 12.14111 12.14223
## [497] 12.14392 12.14619 12.14907 12.15346 12.16010 12.16877 12.17926 12.19136
## [505] 12.20485 12.21952 12.23515 12.25154 12.26846 12.28571 12.30308 12.32033
## [513] 12.33728 12.35369 12.36936 12.38407 12.39761 12.40977 12.42033 12.42908
## [521] 12.43723 12.44610 12.45562 12.46574 12.47639 12.48752 12.49906 12.51096
## [529] 12.52316 12.53559 12.54819 12.56092 12.57369 12.58647 12.59917 12.61176
## [537] 12.62415 12.63631 12.64815 12.65963 12.67069 12.68126 12.69129 12.70071
## [545] 12.70946 12.71749 12.72474 12.73114 12.73663 12.74116 12.74466 12.74707
## [553] 12.74834 12.74840 12.74720 12.74467 12.74075 12.73539 12.72852 12.72009
## [561] 12.71061 12.70064 12.69017 12.67919 12.66770 12.65568 12.64313 12.63004
## [569] 12.61639 12.60218 12.58740 12.57205 12.55610 12.53956 12.52241 12.50464
## [577] 12.48635 12.46761 12.44841 12.42874 12.40857 12.38790 12.36671 12.34499
## [585] 12.32271 12.29986 12.27643 12.25240 12.22776 12.20249 12.17658 12.15001
## [593] 12.12276 12.09482 12.06618 12.03688 12.00698 11.97647 11.94536 11.91365
## [601] 11.88134 11.84844 11.81494 11.78084 11.74614 11.71086 11.67498 11.63851
## [609] 11.60144 11.56379 11.52555 11.48672 11.44731 11.40731 11.36673 11.32556
## [617] 11.28381 11.24148 11.19857 11.15509 11.11102 11.06638 11.02116 10.97537
## [625] 10.92900
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 625)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.63043 12.62567 12.62103 12.61650 12.61208 12.60777 12.60355 12.59943
## [9] 12.59540 12.59147 12.58762 12.58386 12.58018 12.57657 12.57305 12.56959
## [17] 12.56620 12.56288 12.55961 12.55641 12.55326 12.55016 12.54712 12.54411
## [25] 12.54115 12.53823 12.53535 12.53249 12.52967 12.52688 12.52412 12.52141
## [33] 12.51873 12.51611 12.51353 12.51100 12.50853 12.50612 12.50377 12.50149
## [41] 12.49927 12.49713 12.49506 12.49307 12.49116 12.48933 12.48759 12.48594
## [49] 12.48439 12.48293 12.48157 12.48032 12.47917 12.47813 12.47721 12.47640
## [57] 12.47571 12.47514 12.47470 12.47438 12.47420 12.47416 12.47425 12.47448
## [65] 12.47483 12.47528 12.47583 12.47648 12.47722 12.47807 12.47902 12.48007
## [73] 12.48122 12.48247 12.48383 12.48528 12.48685 12.48852 12.49029 12.49217
## [81] 12.49416 12.49625 12.49845 12.50076 12.50318 12.50570 12.50834 12.51109
## [89] 12.51395 12.51692 12.52001 12.52320 12.52651 12.52994 12.53348 12.53713
## [97] 12.54091 12.54479 12.54880 12.55293 12.55717 12.56154 12.56601 12.57059
## [105] 12.57527 12.58005 12.58491 12.58986 12.59489 12.60000 12.60517 12.61041
## [113] 12.61571 12.62106 12.62646 12.63191 12.63739 12.64291 12.64845 12.65402
## [121] 12.65960 12.66520 12.67080 12.67641 12.68201 12.68760 12.69318 12.69875
## [129] 12.70479 12.71177 12.71956 12.72808 12.73722 12.74688 12.75697 12.76737
## [137] 12.77799 12.78872 12.79948 12.81014 12.82063 12.83082 12.84063 12.84995
## [145] 12.85868 12.86671 12.87396 12.88276 12.89518 12.91072 12.92886 12.94910
## [153] 12.97092 12.99383 13.01731 13.04085 13.06395 13.08609 13.10677 13.12548
## [161] 13.14170 13.15494 13.16468 13.17260 13.18069 13.18892 13.19725 13.20565
## [169] 13.21409 13.22252 13.23093 13.23926 13.24750 13.25560 13.26353 13.27126
## [177] 13.27875 13.28597 13.29288 13.29946 13.30566 13.31145 13.31680 13.32168
## [185] 13.32605 13.32987 13.33311 13.33575 13.33773 13.33904 13.33964 13.33948
## [193] 13.33854 13.33679 13.33419 13.33070 13.32629 13.32094 13.31459 13.30723
## [201] 13.29881 13.28930 13.27867 13.26688 13.25390 13.23818 13.21849 13.19526
## [209] 13.16893 13.13993 13.10870 13.07568 13.04129 13.00598 12.97018 12.93432
## [217] 12.89885 12.86420 12.83080 12.79909 12.76950 12.74247 12.71844 12.69784
## [225] 12.67714 12.65286 12.62555 12.59572 12.56390 12.53063 12.49644 12.46185
## [233] 12.42739 12.39360 12.36100 12.33013 12.30150 12.27566 12.25313 12.23444
## [241] 12.21819 12.20260 12.18765 12.17331 12.15954 12.14631 12.13361 12.12138
## [249] 12.10961 12.09827 12.08731 12.07673 12.06648 12.05653 12.04685 12.03742
## [257] 12.02820 12.01917 12.01029 12.00328 11.99959 11.99878 12.00045 12.00415
## [265] 12.00946 12.01595 12.02320 12.03077 12.03824 12.04518 12.05116 12.05576
## [273] 12.05855 12.05910 12.05699 12.05452 12.05415 12.05561 12.05863 12.06297
## [281] 12.06837 12.07456 12.08128 12.08829 12.09531 12.10210 12.10839 12.11392
## [289] 12.11844 12.12168 12.12339 12.12331 12.12119 12.11675 12.11079 12.10429
## [297] 12.09729 12.08986 12.08203 12.07386 12.06540 12.05670 12.04780 12.03877
## [305] 12.02964 12.02046 12.01130 12.00219 11.99318 11.98434 11.97451 11.96263
## [313] 11.94884 11.93330 11.91614 11.89752 11.87759 11.85648 11.83436 11.81135
## [321] 11.78763 11.76332 11.73858 11.71356 11.68840 11.66325 11.63825 11.61356
## [329] 11.58933 11.56569 11.54280 11.52080 11.49985 11.48008 11.46166 11.44471
## [337] 11.42940 11.41586 11.40425 11.39472 11.38740 11.38246 11.38002 11.37911
## [345] 11.37864 11.37866 11.37919 11.38027 11.38193 11.38422 11.38716 11.39079
## [353] 11.39515 11.40027 11.40619 11.41293 11.42054 11.42905 11.43850 11.45021
## [361] 11.46531 11.48348 11.50440 11.52778 11.55331 11.58067 11.60956 11.63966
## [369] 11.67068 11.70229 11.73420 11.76608 11.79764 11.82857 11.85855 11.88728
## [377] 11.91445 11.93975 11.96287 11.98350 12.00457 12.02897 12.05629 12.08612
## [385] 12.11808 12.15175 12.18675 12.22266 12.25910 12.29566 12.33194 12.36753
## [393] 12.40206 12.43510 12.46626 12.49515 12.52135 12.54448 12.56414 12.58281
## [401] 12.60308 12.62466 12.64723 12.67052 12.69421 12.71802 12.74165 12.76481
## [409] 12.78719 12.80850 12.82845 12.84674 12.86307 12.87715 12.88868 12.89862
## [417] 12.90812 12.91715 12.92569 12.93371 12.94118 12.94809 12.95440 12.96010
## [425] 12.96515 12.96953 12.97322 12.97619 12.97841 12.97987 12.98053 12.98037
## [433] 12.97936 12.97749 12.97472 12.96926 12.95969 12.94657 12.93047 12.91194
## [441] 12.89155 12.86985 12.84740 12.82478 12.80253 12.78121 12.76140 12.74364
## [449] 12.72850 12.71654 12.70541 12.69250 12.67791 12.66179 12.64425 12.62543
## [457] 12.60545 12.58445 12.56254 12.53986 12.51654 12.49270 12.46847 12.44398
## [465] 12.41935 12.39472 12.37021 12.34595 12.32206 12.29868 12.27593 12.25394
## [473] 12.23284 12.21275 12.19381 12.17613 12.15986 12.14511 12.13202 12.12070
## [481] 12.11130 12.10393 12.09873 12.09451 12.09007 12.08552 12.08096 12.07650
## [489] 12.07225 12.06831 12.06479 12.06180 12.05943 12.05781 12.05702 12.05719
## [497] 12.05841 12.06079 12.06445 12.07016 12.07850 12.08923 12.10211 12.11691
## [505] 12.13338 12.15129 12.17041 12.19049 12.21129 12.23259 12.25414 12.27570
## [513] 12.29704 12.31792 12.33810 12.35735 12.37543 12.39209 12.40711 12.42025
## [521] 12.43307 12.44724 12.46267 12.47926 12.49692 12.51554 12.53503 12.55530
## [529] 12.57625 12.59779 12.61981 12.64222 12.66492 12.68782 12.71083 12.73383
## [537] 12.75675 12.77948 12.80193 12.82400 12.84559 12.86662 12.88697 12.90656
## [545] 12.92528 12.94305 12.95977 12.97534 12.98966 13.00264 13.01418 13.02419
## [553] 13.03257 13.03922 13.04404 13.04695 13.04784 13.04662 13.04319 13.03745
## [561] 13.03023 13.02240 13.01392 13.00480 12.99500 12.98452 12.97333 12.96141
## [569] 12.94875 12.93532 12.92112 12.90612 12.89030 12.87365 12.85614 12.83777
## [577] 12.81864 12.79888 12.77847 12.75739 12.73564 12.71318 12.69002 12.66612
## [585] 12.64149 12.61609 12.58992 12.56297 12.53520 12.50661 12.47719 12.44691
## [593] 12.41577 12.38374 12.35082 12.31703 12.28245 12.24706 12.21088 12.17389
## [601] 12.13611 12.09754 12.05817 12.01801 11.97706 11.93532 11.89279 11.84947
## [609] 11.80536 11.76047 11.71480 11.66835 11.62111 11.57309 11.52430 11.47472
## [617] 11.42438 11.37325 11.32135 11.26869 11.21525 11.16104 11.10606 11.05031
## [625] 10.99380
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 625)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.02802 12.02290 12.01791 12.01304 12.00827 12.00361 11.99905 11.99458
## [9] 11.99020 11.98590 11.98168 11.97753 11.97344 11.96941 11.96543 11.96150
## [17] 11.95761 11.95375 11.94993 11.94612 11.94234 11.93857 11.93481 11.93104
## [25] 11.92728 11.92350 11.91970 11.91589 11.91204 11.90816 11.90424 11.90028
## [33] 11.89627 11.89220 11.88806 11.88386 11.87962 11.87537 11.87111 11.86686
## [41] 11.86262 11.85839 11.85418 11.85000 11.84585 11.84173 11.83766 11.83364
## [49] 11.82967 11.82576 11.82191 11.81814 11.81444 11.81082 11.80729 11.80386
## [57] 11.80052 11.79729 11.79417 11.79117 11.78828 11.78553 11.78290 11.78042
## [65] 11.77808 11.77589 11.77385 11.77198 11.77027 11.76873 11.76737 11.76596
## [73] 11.76428 11.76235 11.76019 11.75784 11.75530 11.75261 11.74980 11.74687
## [81] 11.74386 11.74080 11.73770 11.73458 11.73148 11.72841 11.72541 11.72248
## [89] 11.71966 11.71697 11.71444 11.71208 11.70992 11.70799 11.70631 11.70490
## [97] 11.70378 11.70298 11.70253 11.70244 11.70274 11.70345 11.70461 11.70622
## [105] 11.70832 11.71093 11.71400 11.71746 11.72131 11.72553 11.73011 11.73504
## [113] 11.74029 11.74586 11.75175 11.75792 11.76437 11.77110 11.77808 11.78530
## [121] 11.79275 11.80041 11.80828 11.81634 11.82458 11.83299 11.84155 11.85025
## [129] 11.85907 11.86801 11.87705 11.88618 11.89539 11.90637 11.92056 11.93754
## [137] 11.95689 11.97819 12.00103 12.02499 12.04964 12.07458 12.09937 12.12361
## [145] 12.14688 12.16875 12.18882 12.20665 12.22184 12.23762 12.25720 12.28015
## [153] 12.30600 12.33432 12.36464 12.39653 12.42953 12.46318 12.49706 12.53069
## [161] 12.56364 12.59545 12.62568 12.65387 12.67958 12.70236 12.72175 12.73731
## [169] 12.75069 12.76380 12.77664 12.78917 12.80138 12.81325 12.82476 12.83587
## [177] 12.84658 12.85686 12.86670 12.87606 12.88493 12.89328 12.90110 12.90837
## [185] 12.91505 12.92114 12.92661 12.93144 12.93561 12.93909 12.94187 12.94392
## [193] 12.94522 12.94576 12.94551 12.94444 12.94255 12.93980 12.93617 12.93165
## [201] 12.92622 12.91984 12.91251 12.90419 12.89487 12.88453 12.87315 12.86070
## [209] 12.84716 12.83251 12.81425 12.79037 12.76165 12.72885 12.69272 12.65403
## [217] 12.61353 12.57200 12.53019 12.48886 12.44877 12.41069 12.37537 12.34359
## [225] 12.31609 12.29364 12.27252 12.24875 12.22263 12.19451 12.16471 12.13357
## [233] 12.10140 12.06854 12.03531 12.00205 11.96909 11.93675 11.90537 11.87526
## [241] 11.84677 11.82021 11.79592 11.77423 11.75547 11.73888 11.72348 11.70920
## [249] 11.69597 11.68373 11.67241 11.66196 11.65232 11.64341 11.63518 11.62756
## [257] 11.62049 11.61391 11.60775 11.60196 11.59646 11.59120 11.58610 11.58112
## [265] 11.57619 11.57123 11.56620 11.56102 11.55564 11.54999 11.54400 11.53763
## [273] 11.53079 11.52343 11.51549 11.50810 11.50233 11.49800 11.49492 11.49291
## [281] 11.49179 11.49138 11.49151 11.49198 11.49262 11.49324 11.49367 11.49373
## [289] 11.49323 11.49199 11.48983 11.48658 11.48204 11.47604 11.46898 11.46144
## [297] 11.45348 11.44517 11.43658 11.42779 11.41885 11.40984 11.40083 11.39189
## [305] 11.38308 11.37448 11.36616 11.35818 11.35061 11.34352 11.33587 11.32665
## [313] 11.31597 11.30396 11.29074 11.27644 11.26117 11.24505 11.22822 11.21079
## [321] 11.19289 11.17463 11.15614 11.13755 11.11896 11.10052 11.08233 11.06453
## [329] 11.04723 11.03055 11.01462 10.99957 10.98550 10.97255 10.96084 10.95049
## [337] 10.94162 10.93435 10.92881 10.92512 10.92340 10.92378 10.92637 10.93130
## [345] 10.93846 10.94762 10.95855 10.97103 10.98482 10.99970 11.01543 11.03180
## [353] 11.04857 11.06551 11.08240 11.09900 11.11510 11.13045 11.14484 11.16019
## [361] 11.17843 11.19928 11.22246 11.24771 11.27475 11.30330 11.33310 11.36387
## [369] 11.39534 11.42724 11.45928 11.49121 11.52275 11.55362 11.58355 11.61227
## [377] 11.63950 11.66498 11.68842 11.70956 11.73068 11.75403 11.77934 11.80631
## [385] 11.83467 11.86411 11.89436 11.92513 11.95613 11.98707 12.01768 12.04765
## [393] 12.07671 12.10457 12.13094 12.15553 12.17806 12.19825 12.21579 12.23249
## [401] 12.25017 12.26863 12.28767 12.30707 12.32664 12.34615 12.36542 12.38422
## [409] 12.40236 12.41962 12.43580 12.45070 12.46410 12.47581 12.48561 12.49389
## [417] 12.50122 12.50766 12.51324 12.51803 12.52207 12.52542 12.52812 12.53024
## [425] 12.53181 12.53289 12.53354 12.53380 12.53373 12.53337 12.53278 12.53200
## [433] 12.53110 12.53012 12.52912 12.52667 12.52157 12.51418 12.50484 12.49392
## [441] 12.48176 12.46873 12.45516 12.44143 12.42788 12.41486 12.40274 12.39186
## [449] 12.38258 12.37526 12.36838 12.36027 12.35100 12.34065 12.32932 12.31706
## [457] 12.30398 12.29014 12.27562 12.26052 12.24490 12.22885 12.21245 12.19577
## [465] 12.17891 12.16194 12.14494 12.12799 12.11117 12.09457 12.07826 12.06232
## [473] 12.04683 12.03188 12.01754 12.00390 11.99103 11.97902 11.96795 11.95789
## [481] 11.94894 11.94115 11.93463 11.92816 11.92061 11.91221 11.90317 11.89370
## [489] 11.88401 11.87433 11.86486 11.85582 11.84743 11.83989 11.83343 11.82825
## [497] 11.82458 11.82262 11.82259 11.82426 11.82722 11.83136 11.83659 11.84281
## [505] 11.84992 11.85783 11.86644 11.87566 11.88539 11.89553 11.90599 11.91668
## [513] 11.92748 11.93832 11.94909 11.95970 11.97005 11.98005 11.98959 11.99859
## [521] 12.00794 12.01855 12.03034 12.04322 12.05712 12.07195 12.08763 12.10408
## [529] 12.12122 12.13897 12.15724 12.17596 12.19503 12.21439 12.23394 12.25361
## [537] 12.27332 12.29297 12.31250 12.33182 12.35085 12.36950 12.38770 12.40537
## [545] 12.42241 12.43875 12.45432 12.46902 12.48277 12.49550 12.50712 12.51755
## [553] 12.52671 12.53451 12.54088 12.54574 12.54899 12.55057 12.55038 12.54835
## [561] 12.54533 12.54217 12.53882 12.53522 12.53133 12.52707 12.52241 12.51727
## [569] 12.51161 12.50537 12.49849 12.49093 12.48261 12.47349 12.46351 12.45261
## [577] 12.44110 12.42928 12.41713 12.40460 12.39168 12.37833 12.36452 12.35021
## [585] 12.33538 12.32000 12.30403 12.28745 12.27021 12.25230 12.23368 12.21432
## [593] 12.19418 12.17324 12.15147 12.12894 12.10575 12.08192 12.05743 12.03230
## [601] 12.00653 11.98012 11.95307 11.92538 11.89706 11.86811 11.83854 11.80834
## [609] 11.77752 11.74608 11.71403 11.68136 11.64808 11.61419 11.57970 11.54460
## [617] 11.50891 11.47261 11.43573 11.39825 11.36018 11.32153 11.28229 11.24247
## [625] 11.20208
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")